<?php

/*
 * 顾客表
 */

/**
 * Description of customers_model
 *
 * @author Jack.Y
 */
class Customers_Model extends CI_Model {
    /*
     * =======================基础数据库操作=================================
     */

//重载CI_Model
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('Base_Model');
        $this->load->model('ChooseContent_model');
    }

    /*
     * Insert_Product
     * 写入数据库
     */

    function Insert_Customers($data = FALSE) {
        $data['cu_id'] = $this->Base_Model->GetID('cu_id', 'db_customers');
        $this->db->insert('db_customers', $data);
        return $data['cu_id'];
    }

    /*
     * GetForState
     * 获取列表数据
     * 参数：状态
     */

    function GetListForState($str = FALSE) {
        $this->db->select("db_customers.cu_id,db_customers.cu_groom,db_customers.cu_bride,db_customers.cu_marrydate,db_customers.cu_takedate,db_customers.cu_address,db_customers.se_id,db_customers.cu_total,db_customers.cu_paid,db_customers.cu_unpaid,db_customers.cu_state,db_series.se_name,db_user.u_name,db_customers.cu_cellphone,db_customers.cu_phone");
        $this->db->from('db_customers');
        $this->db->join('db_series', 'db_customers.se_id = db_series.se_id');
        $this->db->join('db_user', 'db_customers.u_id = db_user.u_id');
        $this->db->where('db_customers.cu_state', $str);
        $this->db->order_by("cu_id", "desc");
        $query = $this->db->get();
//返回结果
        return $query->result_array();
    }

    /*
     * GetListForMan
     * 为主页获取快到期数据
     * 参数：状态
     */

    function GetListForMan($limit = FALSE) {
        $this->db->select("db_customers.cu_id,db_customers.cu_groom,db_customers.cu_bride,db_customers.cu_marrydate,db_customers.cu_takedate,db_customers.cu_address,db_customers.se_id,db_customers.cu_total,db_customers.cu_paid,db_customers.cu_unpaid,db_customers.cu_state,db_series.se_name,db_user.u_name,db_customers.cu_cellphone,db_customers.cu_phone");
        $this->db->from('db_customers');
        $this->db->join('db_series', 'db_customers.se_id = db_series.se_id');
        $this->db->join('db_user', 'db_customers.u_id = db_user.u_id');
        $this->db->where('db_customers.cu_state', OBJ_STATE_VALID);
        $this->db->where('date_add(CURDATE(), INTERVAL 7 DAY) >= date(cu_takedate)');
        $this->db->where('NOW() <= date(cu_takedate)');
        $this->db->order_by("cu_takedate", "asc");
        if ($limit) {
            $this->db->limit(6);
        }
        $query = $this->db->get();
//返回结果
        return $query->result_array();
    }

    /*
     * GetListForAnniversary
     * 为主页获取周年数据
     * 参数：状态
     */

    function GetListForAnniversary($limit = FALSE) {
        $this->db->select("db_customers.cu_id,db_customers.cu_groom,db_customers.cu_bride,db_customers.cu_marrydate,db_customers.cu_takedate,db_customers.cu_address,db_customers.se_id,db_customers.cu_total,db_customers.cu_paid,db_customers.cu_unpaid,db_customers.cu_state,db_series.se_name,db_user.u_name,db_customers.cu_cellphone,db_customers.cu_phone");
        $this->db->from('db_customers');
        $this->db->join('db_series', 'db_customers.se_id = db_series.se_id');
        $this->db->join('db_user', 'db_customers.u_id = db_user.u_id');
        $this->db->where('db_customers.cu_state', OBJ_STATE_VALID);
        $this->db->where("DATE_FORMAT(cu_marrydate,'%m%d')=DATE_FORMAT(now(),'%m%d')");
        $this->db->order_by("db_customers.cu_id", "asc");
        if ($limit) {
            $this->db->limit(10);
        }
        $query = $this->db->get();
//返回结果
        return $query->result_array();
    }

    /*
     * GetListForAnniversary
     * 为主页获取最新数据
     * 参数：状态
     */

    function GetListForNew($limit = FALSE) {
        $this->db->select("db_customers.cu_id,db_customers.cu_groom,db_customers.cu_bride,db_customers.cu_marrydate,db_customers.cu_takedate,db_customers.cu_address,db_customers.se_id,db_customers.cu_total,db_customers.cu_paid,db_customers.cu_unpaid,db_customers.cu_state,db_series.se_name,db_user.u_name,db_customers.cu_cellphone,db_customers.cu_phone");
        $this->db->from('db_customers');
        $this->db->join('db_series', 'db_customers.se_id = db_series.se_id');
        $this->db->join('db_user', 'db_customers.u_id = db_user.u_id');
        $this->db->where('db_customers.cu_state', OBJ_STATE_VALID);
        $this->db->order_by("cu_id", "desc");
        if ($limit) {
            $this->db->limit(10);
        }
        $query = $this->db->get();
//返回结果
        return $query->result_array();
    }

    /*
     * GetEntity
     * 获取单挑数据
     * 参数：cu_id
     */

    function GetEntity($str = FALSE) {
        if ($str) {
            $query = $this->db->get_where('db_customers', array('cu_id' => $str));
//返回结果
            return $query->result_array();
        } else {
            $Array = array(array("cu_id" => '', "cu_groom" => '', "cu_bride" => '', "cu_marrydate" => '', "cu_takedate" => '', "cu_address" => '', "se_id" => '', "cu_total" => '', "cu_paid" => '', "cu_unpaid" => '', "cu_cellphone" => '', "cu_phone" => ''));
            return $Array;
        }
    }

    /*
     * UpdateEntity
     * 更新单挑数据
     */

    function UpdateEntity($id = FALSE, $data = FALSE) {

        $this->db->where('cu_id', $id);
        $result = $this->db->update('db_customers', $data);
        //返回结果
        return $result;
    }

    /*
     * UpdateEntity
     * 删除
     */

    function Del($id = FALSE, $data = FALSE) {

        $this->db->where('cu_id', $id);
        $result = $this->db->update('db_customers', $data);
        $this->Base_Model->log("删除订单", $id);
        //返回结果
        return $result;
    }

    /*
     * =======================综合性数据库操作=================================
     */

    function Insert_Customer_Product($data = FALSE, $array = FALSE) {
        $this->db->trans_start(); //开启事务
        $resultId = $this->Insert_Customers($data);
        if ($array[0] != "") {
            $this->ChooseContent_model->Insert_ChooseContent($array, $resultId);
        }
        $this->db->trans_complete();
        $this->Base_Model->logs("创建订单", $data, $array);
    }

    /*
     * Update_Customer_Product
     * 更新Customer和ChooseContent
     */

    function Update_Customer_Product($id = FALSE, $data = FALSE, $array = FALSE) {
        $this->db->trans_start(); //开启事务
        $this->UpdateEntity($id, $data);
        $this->ChooseContent_model->DelDate($id); //删除原有数据
        if ($array[0] != "") {
            $this->ChooseContent_model->Insert_ChooseContent($array, $id); //重新创建
        }
        $this->db->trans_complete();
        $this->Base_Model->logs("更新订单", $data, $array);
    }

    /*
     * =======================综合性数据库操作=================================
     */
}

?>
